<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/10/25
 * Time: 14:27
 */

namespace app\mec\controller;



use think\Db;
use think\Request;

class Store extends Base
{
    /**
     * 首页轮播图展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            $fids = $this->getSonIDS();
            $where['store_fid'] = ['in',$fids];
            if(isset($params['level'])&&$params['level']!="")
            {
                if($params['level'] == -1){
                    $params['level'] = 0;
                }
                $where['store_level'] = $params['level'];
            }
            if(isset($params['shi'])&&$params['shi']!="")
            {
                $where['store_shi'] = $params['shi'];
            }
            if(isset($params['banner_title']) && $params['banner_title'] != ''){
                $where['store_name|store_username|store_address|store_phone'] = ['like', '%' . $params['banner_title'] . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_store')->alias('s')
                ->join('up_area a','s.store_shi=a.district_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_store')->alias('s')
                ->join('up_area a','s.store_shi=a.district_id','LEFT')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('store_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $citys = Db::table('up_area')->where('level',2)->select();
        $this->assign('citys',$citys);
        $level = Db::name('fenxiao')->where('fen_id',session('mid'))->value('fen_level');
        $this->assign('ll',$level);
        return $this->fetch();
    }
    
    /**
     * 添加轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            $level = Db::name('fenxiao')->where('fen_id',session('mid'))->value('fen_level');
            $params = input('param.');
            $insert = [
                'store_name' => $params['store_name'],
                'store_username' => $params['store_username'],
                'store_shi' => $params['store_shi'],
                'store_address' => $params['store_address'],
                'store_phone' => $params['store_phone'],
                'store_image' => $params['store_image'],
                'store_fid' => session('mid'),
                'store_level' => $level,
                'store_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_store')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $citys = Db::table('up_area')->where('level',2)->select();
        $this->assign('citys',$citys);
        return $this->fetch('store/banneradd');
    }
    
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        $banner_id = input('banner_id');
        if(Request::instance()->isPost()){
            $params = input('param.');
            $update = [
                'store_name' => $params['store_name'],
                'store_username' => $params['store_username'],
                'store_shi' => $params['store_shi'],
                'store_address' => $params['store_address'],
                'store_phone' => $params['store_phone'],
                'store_image' => $params['store_image'],
                'store_time' => date('Y-m-d H:i:s'),
            ];
            
            $res = Db::table('up_store')->where('store_id', $banner_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner = Db::table('up_store')
            ->where('store_id', $banner_id)
            ->find();
        $this->assign([
            'banner_id' => $banner_id,
            'banner' => $banner
        ]);
        $citys = Db::table('up_area')->where('level',2)->select();
        $this->assign('citys',$citys);
        return $this->fetch('store/banneredit');
    }
    
    /**
     * 轮播图详情
     * @return mixed
     */
    public function bannerDetail(){
        $banner_id = input('banner_id');
        $banner = Db::table('up_store')->alias('s')
            ->join('up_area a','s.store_shi=a.district_id','LEFT')
            ->where('store_id', $banner_id)
            ->find();
        $this->assign([
            'banner' => $banner
        ]);
        return $this->fetch('store/bannerdetail');
    }
    
    /**
     * 删除轮播图
     * @return \think\response\Json
     */
    public function bannerDel(){
        $banner_id = input('id');
        $res = Db::table('up_store')->where('store_id', $banner_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}